package com.kuang;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 *
 */
public class TestTX {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("101.200.173.234", 6379);
        jedis.auth("123456");
        System.out.println(jedis.ping());

        //jedis操作
        jedis.flushDB();
        Transaction multi = jedis.multi();
        try {
            multi.set("user1", "这里是user1");
            multi.incr("user1");//这里有一个运行时异常
            multi.set("user2", "这里是user2");
            multi.exec();//执行事务
        } catch (Exception e) {
            multi.discard();//放弃事务
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();//关闭事务
        }
    }
}
